package com.nic.view.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;

import com.nic.view.R;
import com.nic.view.model.CommonChooseBean;


/**
 * 多项点单选
 * Created by Administrator on 2018/7/13.
 */

public class SingleChooseDotAdapter extends CommonAdapter<CommonChooseBean> {
    private OnClickItemListener listener;
    private int currtIndex = -1;


    public void setListener(OnClickItemListener listener) {
        this.listener = listener;
    }

    @Override
    public View getBindView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_choose_dot, null);
            holder = new ViewHolder();
            holder.checkBox = (CheckBox) convertView.findViewById(R.id.cb_single);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final CommonChooseBean chooseBean = getList().get(position);
        holder.checkBox.setText("" + chooseBean.getName());
        if (currtIndex != -1 && currtIndex == position) {
            chooseBean.setCheck(true);
        } else {
            chooseBean.setCheck(false);
        }
        holder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currtIndex = position;
                for (int i = 0; i < getList().size(); i++) {
                    CommonChooseBean bean = getList().get(i);
                    if (currtIndex == i) {
                        bean.setCheck(true);
                    } else {
                        bean.setCheck(false);
                    }
                }
                notifyDataSetChanged();
                if (listener != null) {
                    listener.onItemClick(position);
                }
            }
        });
        if (chooseBean.isCheck()) {
            holder.checkBox.setChecked(true);
            holder.checkBox.setTextColor(0xFF1883C7);
        } else {
            holder.checkBox.setChecked(false);
            holder.checkBox.setTextColor(0xFF4D4D4D);
        }

        return convertView;
    }

    public void setSelected(int selected) {
        this.currtIndex = selected;
        notifyDataSetChanged();
    }

    class ViewHolder {
        CheckBox checkBox;
    }

    public interface OnClickItemListener {
        void onItemClick(int position);
    }
}
